home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1SSMP2U (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  3.9 KB  |  83 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.ButtonModel;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.UIManager;
  7. import com.sun.java.swing.plaf.ComponentUI;
  8. import com.sun.java.swing.plaf.basic.BasicGraphicsUtils;
  9. import com.sun.java.swing.plaf.basic.BasicToggleButtonUI;
  10. import java.awt.Color;
  11. import java.awt.Component;
  12. import java.awt.Dimension;
  13. import java.awt.FontMetrics;
  14. import java.awt.Graphics;
  15. import java.awt.Insets;
  16. import java.awt.Rectangle;
  17.  
  18. public class MetalToggleButtonUI extends BasicToggleButtonUI {
  19.    private static Color selectedColor;
  20.    private static Color disabledTextColor;
  21.    private static Color focusColor;
  22.    protected static final Insets defaultMargin = new Insets(2, 14, 2, 14);
  23.    private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
  24.  
  25.    public static ComponentUI createUI(JComponent b) {
  26.       return metalToggleButtonUI;
  27.    }
  28.  
  29.    public Insets getDefaultMargin(AbstractButton b) {
  30.       return defaultMargin;
  31.    }
  32.  
  33.    public void installUI(JComponent c) {
  34.       super.installUI(c);
  35.       selectedColor = UIManager.getColor("ToggleButton.selected");
  36.       disabledTextColor = UIManager.getColor("ToggleButton.disabledText");
  37.       focusColor = UIManager.getColor("ToggleButton.focus");
  38.    }
  39.  
  40.    protected void paintButtonPressed(Graphics g, AbstractButton b) {
  41.       Dimension size = ((Component)b).getSize();
  42.       g.setColor(selectedColor);
  43.       g.fillRect(0, 0, size.width, size.height);
  44.    }
  45.  
  46.    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
  47.       Rectangle focusRect = new Rectangle();
  48.       String text = b.getText();
  49.       boolean isIcon = b.getIcon() != null;
  50.       if (text != null & !text.equals("")) {
  51.          if (!isIcon) {
  52.             focusRect.setBounds(textRect);
  53.          } else {
  54.             focusRect.setBounds(iconRect.union(textRect));
  55.          }
  56.       } else if (isIcon) {
  57.          focusRect.setBounds(iconRect);
  58.       }
  59.  
  60.       g.setColor(focusColor);
  61.       g.drawRect(focusRect.x - 1, focusRect.y - 1, focusRect.width + 1, focusRect.height + 1);
  62.    }
  63.  
  64.    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  65.       AbstractButton b = (AbstractButton)c;
  66.       ButtonModel model = b.getModel();
  67.       FontMetrics fm = g.getFontMetrics();
  68.       if (model.isEnabled()) {
  69.          g.setColor(((Component)b).getForeground());
  70.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  71.       } else {
  72.          if (model.isSelected()) {
  73.             g.setColor(UIManager.getColor("Button.background"));
  74.          } else {
  75.             g.setColor(UIManager.getColor("Button.disabledText"));
  76.          }
  77.  
  78.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  79.       }
  80.  
  81.    }
  82. }
  83.